package master;

import java.nio.channels.SeekableByteChannel;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chenyw
 * @date 2022/9/6 9:54
 * 汇总区间
 */
public class M228summaryRanges {
    public static void main(String[] args) {
        summaryRanges(new int[] {
                0,1,2,4,5,7
        });
    }
    public static List<String> summaryRanges(int[] nums) {
        List<String> res = new ArrayList<>();
        if (nums == null || nums.length == 0) {
            return res;
        }
        if (nums.length == 1) {
            res.add(String.valueOf(nums[0]));
            return res;
        }
        int index = 0;
        for (int i = 0; i < nums.length; i++) {
            if (i + 1 < nums.length && nums[i] == nums[i + 1] + 1) {
                continue;
            } else {
                if (index != i) {
                    res.add(nums[index] + "->" + nums[i]);
                } else {
                    res.add(String.valueOf(nums[i]));
                }
                index = i +1;
            }
        }
        return res;

    }
}
